<template>
  <div class="mod-demo__line" style="overflow-y: scroll;height: 800px;">
    <el-descriptions>
      <el-descriptions-item label="项目名称">{{routeParams.projectName}}</el-descriptions-item>
    </el-descriptions>
    <div style="height: 10px"></div>
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
        <el-input style="display: none" v-model="state.dataForm.projectId" placeholder="" clearable></el-input>
      <el-form-item label="线路名称">
        <el-input v-model="state.dataForm.name" placeholder="请输入线路名称" clearable></el-input>
      </el-form-item >
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>

      <el-form-item>
        <el-button  type="primary" @click="copyLineHandle()">导入其他项目线路</el-button>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="addOrUpdateHandle()">新增线路</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
              <el-table-column prop="name" label="线路名称" header-align="center" align="center"></el-table-column>
              <el-table-column prop="projectName" label="所属项目" header-align="center" align="center"></el-table-column>
              <el-table-column prop="createDate" label="创建日期" header-align="center" align="center"></el-table-column>
              <el-table-column prop="equipmentNum" label="定位点数" header-align="center" align="center"></el-table-column>
              <el-table-column prop="startDate" label="施工日期" header-align="center" align="center"></el-table-column>
              <el-table-column prop="notes" label="备注" header-align="center" align="center"></el-table-column>
            <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button  type="primary" link @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button  type="primary" link
                      @click="goToEquipment(scope.row)">定位点管理
          </el-button>
          <el-button  type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList">确定</add-or-update>
    <copyLine ref="copyLineRef" @refreshDataList="state.getDataList">确定</copyLine>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { reactive, ref, toRefs ,watch} from "vue";
import AddOrUpdate from "./line-add-or-update.vue";
import {useRoute,useRouter} from "vue-router";
import copyLine from "./line-copy.vue";

const view = reactive({
  deleteIsBatch: true,
  getDataListURL: "/sysproject/line/page",
  getDataListIsPage: true,
  exportURL: "/sysproject/line/export",
  deleteURL: "/sysproject/line",
  dataForm:{projectId:"",}
});
const route = useRoute();
const routeParams = ref({});
routeParams.value = route.params;


const state = reactive({ ...useView(view), ...toRefs(view) });
const copyLineRef = ref();

const copyLineHandle = (row?:{}) => {
  let rows ={};
  if(undefined == row){
    rows = route.params;
  }
  copyLineRef.value.init(rows);
};

const addOrUpdateRef = ref();
const addOrUpdateHandle = (row?:{}) => {
  let rows ={};
  if(undefined == row){
    rows = route.params;
  }else{
    rows = Object.assign(rows?row:{},route.params);
  }
  addOrUpdateRef.value.init(rows);
};

routeParams.value = route.params;
if(state.dataForm.projectId == ""){
  if(null !=routeParams.value.projectId){
    console.log("获取参数不为空")
    state.dataForm.projectId = routeParams.value.projectId;
    state.getDataList();
  }else{
    console.log("获取参数为空")
  }
}
watch(() => route.params?route.params:undefined, (newId?:{}) => {
  console.log("开始监视路由变化")
  console.log()
  if(Object.keys(newId).length !== 0){
    if(routeParams.value.projectId != newId.projectId){
      routeParams.value = newId;
      state.dataForm.projectId = routeParams.value.projectId;
      state.getDataList();
    }
  }
})
const router = useRouter();
const goToEquipment = (tiem) => {
  router.replace({name:"equipment",
                     params:{
                            "projectId":state.dataForm.projectId,
                            "lineId":tiem?tiem.id:"",
                             "projectName":routeParams.value.projectName,
                             "lineName":tiem?tiem.name:""
                    }
  });
};
</script>
